Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Node / ReticulumProjects / Sideband.git / files / sbapp / pyogg / opus_file_stream.py

Displaying Raw • Download


sbapp/pyogg/opus_file_stream.py f8b3e3760b831745cf10d97aeae64e9c00a136a9 (f8b3e376) Text, 3.88 KB

Tff7b72import T7ee787ctypes

Tff7b72from T7ee787. Tff7b72import Te6edf3ogg
Tff7b72from T7ee787. Tff7b72import Te6edf3opus
Tff7b72from T7ee787.T7ee787pyogg_error Tff7b72import Te6edf3PyOggError

Tff7b72class T56d364OpusFileStreamTb4b4b4:
Tff7b72def Tff7b72__init__Tb4b4b4(Tff7b72selfTb4b4b4, Te6edf3pathTb4b4b4)Tb4b4b4:
T8b949e"""Opens an OggOpus file as a stream.

path should be a string giving the filename of the file to
open. Unicode file names may not work correctly.

An exception will be raised if the file cannot be opened
correctly.

"""
Te6edf3error Tff7b72= Te6edf3ctypesTff7b72.Td2a8ffc_intTb4b4b4(Tb4b4b4)

Tff7b72selfTff7b72.Td2a8ffof Tff7b72= Te6edf3opusTff7b72.Td2a8ffop_open_fileTb4b4b4(Te6edf3oggTff7b72.Td2a8ffto_char_pTb4b4b4(Te6edf3pathTb4b4b4)Tb4b4b4, Te6edf3ctypesTff7b72.Td2a8ffpointerTb4b4b4(Te6edf3errorTb4b4b4)Tb4b4b4)

Tff7b72if Te6edf3errorTff7b72.Td2a8ffvalue Tff7b72!= T79c0ff0Tb4b4b4:
Tff7b72selfTff7b72.Td2a8ffof Tff7b72= Tff7b72None
Tff7b72raise Te6edf3PyOggErrorTb4b4b4(Ta5d6ff"Ta5d6fffile couldnTa5d6ff'Ta5d6fft be opened or doesnTa5d6ff'Ta5d6fft exist. Error code : Tffd700{}Ta5d6ff"Tff7b72.Td2a8ffformatTb4b4b4(Te6edf3errorTff7b72.Td2a8ffvalueTb4b4b4)Tb4b4b4)

T8b949e#: Number of channels in audio file
Tff7b72selfTff7b72.Td2a8ffchannels Tff7b72= Te6edf3opusTff7b72.Td2a8ffop_channel_countTb4b4b4(Tff7b72selfTff7b72.Td2a8ffofTb4b4b4, Tff7b72-T79c0ff1Tb4b4b4)

T8b949e#: Total PCM Length
Tff7b72selfTff7b72.Td2a8ffpcm_size Tff7b72= Te6edf3opusTff7b72.Td2a8ffop_pcm_totalTb4b4b4(Tff7b72selfTff7b72.Td2a8ffofTb4b4b4, Tff7b72-T79c0ff1Tb4b4b4)

T8b949e#: Number of samples per second (per channel)
Tff7b72selfTff7b72.Td2a8fffrequency Tff7b72= T79c0ff48000

T8b949e# The buffer size should be (per channel) large enough to
T8b949e# hold 120ms (the largest possible Opus frame) at 48kHz.
T8b949e# See https://opus-codec.org/docs/opusfile_api-0.7/group__stream__decoding.html#ga963c917749335e29bb2b698c1cb20a10
Tff7b72selfTff7b72.Td2a8ffbuffer_size Tff7b72= Tff7b72selfTff7b72.Td2a8fffrequency Tff7b72/Tff7b72/ T79c0ff1000 Tff7b72* T79c0ff120 Tff7b72* Tff7b72selfTff7b72.Td2a8ffchannels
Tff7b72selfTff7b72.Td2a8ffBuf Tff7b72= Te6edf3opusTff7b72.Td2a8ffopus_int16 Tff7b72* Tff7b72selfTff7b72.Td2a8ffbuffer_size
Tff7b72selfTff7b72.Td2a8ff_buf Tff7b72= Tff7b72selfTff7b72.Td2a8ffBufTb4b4b4(Tb4b4b4)
Tff7b72selfTff7b72.Td2a8ffbuffer_ptr Tff7b72= Te6edf3ctypesTff7b72.Td2a8ffcastTb4b4b4(
Te6edf3ctypesTff7b72.Td2a8ffpointerTb4b4b4(Tff7b72selfTff7b72.Td2a8ff_bufTb4b4b4)Tb4b4b4,
Te6edf3opusTff7b72.Td2a8ffopus_int16_p
Tb4b4b4)

T8b949e#: Bytes per sample
Tff7b72selfTff7b72.Td2a8ffbytes_per_sample Tff7b72= Te6edf3ctypesTff7b72.Td2a8ffsizeofTb4b4b4(Te6edf3opusTff7b72.Td2a8ffopus_int16Tb4b4b4)

Tff7b72def Tff7b72__del__Tb4b4b4(Tff7b72selfTb4b4b4)Tb4b4b4:
Tff7b72if Tff7b72selfTff7b72.Td2a8ffof Tff7b72is Tff7b72not Tff7b72NoneTb4b4b4:
Te6edf3opusTff7b72.Td2a8ffop_freeTb4b4b4(Tff7b72selfTff7b72.Td2a8ffofTb4b4b4)

Tff7b72def Td2a8ffget_bufferTb4b4b4(Tff7b72selfTb4b4b4)Tb4b4b4:
T8b949e"""Obtains the next frame of PCM samples.

Returns an array of signed 16-bit integers. If the file
is in stereo, the left and right channels are interleaved.

Returns None when all data has been read.

The array that is returned should be either processed or
copied before the next call to :meth:`~get_buffer` or
:meth:`~get_buffer_as_array` as the array's memory is reused for
each call.

"""
T8b949e# Read the next frame
Te6edf3samples_read Tff7b72= Te6edf3opusTff7b72.Td2a8ffop_readTb4b4b4(
Tff7b72selfTff7b72.Td2a8ffofTb4b4b4,
Tff7b72selfTff7b72.Td2a8ffbuffer_ptrTb4b4b4,
Tff7b72selfTff7b72.Td2a8ffbuffer_sizeTb4b4b4,
Tff7b72None
Tb4b4b4)

T8b949e# Check for errors
Tff7b72if Te6edf3samples_read Tff7b72< T79c0ff0Tb4b4b4:
Tff7b72raise Te6edf3PyOggErrorTb4b4b4(
Ta5d6ff"Ta5d6ffFailed to read OpusFileStream. Error Tffd700{:d}Ta5d6ff"Tff7b72.Td2a8ffformatTb4b4b4(Te6edf3samples_readTb4b4b4)
Tb4b4b4)

T8b949e# Check if we've reached the end of the stream
Tff7b72if Te6edf3samples_read Tff7b72== T79c0ff0Tb4b4b4:
Tff7b72return Tff7b72None

T8b949e# Cast the pointer to opus_int16 to an array of the
T8b949e# correct size
Te6edf3result_ptr Tff7b72= Te6edf3ctypesTff7b72.Td2a8ffcastTb4b4b4(
Tff7b72selfTff7b72.Td2a8ffbuffer_ptrTb4b4b4,
Te6edf3ctypesTff7b72.Td2a8ffPOINTERTb4b4b4(Te6edf3opusTff7b72.Td2a8ffopus_int16 Tff7b72* Tb4b4b4(Te6edf3samples_readTff7b72*Tff7b72selfTff7b72.Td2a8ffchannelsTb4b4b4)Tb4b4b4)
Tb4b4b4)

T8b949e# Convert the array to Python bytes
Tff7b72return Tffa657bytesTb4b4b4(Te6edf3result_ptrTff7b72.Td2a8ffcontentsTb4b4b4)

Tff7b72def Td2a8ffget_buffer_as_arrayTb4b4b4(Tff7b72selfTb4b4b4)Tb4b4b4:
T8b949e"""Provides the buffer as a NumPy array.

Note that the underlying data type is 16-bit signed
integers.

Does not copy the underlying data, so the returned array
should either be processed or copied before the next call
to :meth:`~get_buffer` or :meth:`~get_buffer_as_array`.

"""
Tff7b72import T7ee787numpy T8b949e# type: ignore

T8b949e# Read the next samples from the stream
Te6edf3buf Tff7b72= Tff7b72selfTff7b72.Td2a8ffget_bufferTb4b4b4(Tb4b4b4)

T8b949e# Check if we've come to the end of the stream
Tff7b72if Te6edf3buf Tff7b72is Tff7b72NoneTb4b4b4:
Tff7b72return Tff7b72None

T8b949e# Convert the bytes buffer to a NumPy array
Te6edf3array Tff7b72= Te6edf3numpyTff7b72.Td2a8fffrombufferTb4b4b4(
Te6edf3bufTb4b4b4,
Te6edf3dtypeTff7b72=Te6edf3numpyTff7b72.Td2a8ffint16
Tb4b4b4)

T8b949e# Reshape the array
Tff7b72return Te6edf3arrayTff7b72.Td2a8ffreshapeTb4b4b4(
Tb4b4b4(Tffa657lenTb4b4b4(Te6edf3bufTb4b4b4)
Tff7b72/Tff7b72/ Tff7b72selfTff7b72.Td2a8ffbytes_per_sample
Tff7b72/Tff7b72/ Tff7b72selfTff7b72.Td2a8ffchannelsTb4b4b4,
Tff7b72selfTff7b72.Td2a8ffchannelsTb4b4b4)
Tb4b4b4)


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────